package com.cyh.forum.services.impl;

import com.cyh.forum.common.AppResult;
import com.cyh.forum.common.ResultCode;
import com.cyh.forum.dao.BoardMapper;
import com.cyh.forum.exception.ApplicationException;
import com.cyh.forum.model.Board;
import com.cyh.forum.services.IBoradService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class BoradServiceImpl implements IBoradService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        //  非空校验
        if (num <= 0){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //  调用 DAO 层 查询数据库中的数据
        List<Board> result = boardMapper.selectByNum(num);
        return result;
    }

    @Override
    public Board selectByPrimaryKey(Long id) {
        //  参数校验
        if (id <= 0 || id == null){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //  参数校验
        if (id <= 0 || id == null){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //  调用DAO层 根据 版块id 获取数据库 版块数据
        Board board = boardMapper.selectByPrimaryKey(id);
        // 校验版块是否存在
        if (board == null){
            //  打印日志
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString() + ",board id = " + id);
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        Board updateBoard = new Board();
        //  设置版块id
        updateBoard.setId(board.getId());
        //  设置帖子数量
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        //  此时传入的对象只有Id和ArticleCount不为空,因此只会更新这两个数值
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //  更新操作只会影响一行,如果row != 1 则说明出现异常
        if (row != 1){
            log.warn(ResultCode.FAILED.toString() + ",受影响的行数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public void subOneArticleCountById(Long id) {
        //  非空校验
        if (id == null || id <= 0){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //  校验版块是否存在
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null || board.getDeleteState() == 1){
            //  打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + "board id = " + id);
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //  设置更新对象
        Board update = new Board();
        update.setId(id);
        update.setArticleCount(board.getArticleCount() - 1);
        //  校验减一后 帖子数量是否小于零
        if (update.getArticleCount() < 0){
            //  小于零,则设置为零
            update.setArticleCount(0);
        }
        //  调用DAO层
        val row = boardMapper.updateByPrimaryKeySelective(update);
        //  校验是否更新成功
        if (row != 1){
            //  打印日志
            log.warn(ResultCode.FAILED_ARTICLE_DELETE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_DELETE));
        }
    }
}
